// myCourse_package/pages/CourseIntroduction/CourseIntroduction.ts
import {MyCourseDetail} from '../../../api/myCourse'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    title:"",
    formData:{
      serialId:0,
      stuId:0
    },
    //课程详情
    courseDetail:{
    },
    courseProportion:"0",
    colorList:[
      {color:"#FFAAAA",label:"停课"},
      {color:"#9CEF72",label:"未学"},
      {color:"#F7B500",label:"正在学"},
      {color:"#91D0FF",label:"已学过"}
    ]
  },
  // 查看教师信息
  seeTeacherInfo(){
    const deliver = this.data.courseDetail as any
    wx.navigateTo({
      url:`/myCourse_package/pages/teacherIntroduction/teacherIntroduction?instructorId=${deliver.instructorId}`
    })
  },
  // 前往课程订单
  courseOrder(e:any){
   const {courseserialid} = e.currentTarget.dataset
   const token = getApp().getToken()
   if(token){
    wx.navigateTo({
      url:`/myCourse_package/pages/courseOrder/courseOrder?serialId=${courseserialid}`
    })
   }else{
     wx.navigateTo({
       url:"/pages/login/login"
     })
   }

  },
  // 前往学前评价
  studyEvaluate(){
    const detail = this.data.courseDetail as any
    let type = "学前评价"
    if(detail.preComment !== '0'){
      wx.navigateTo({
        url:`/myCourse_package/pages/courseEvaluate/courseEvaluate?seriesId=${detail.courseSerialId}&stuId=${detail.stuId}&type=${type}`
      })
    } 
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(data:any) {
   const {title,stuId,serialId} = data
  this.setData({
    title,
    formData:{
      serialId,
      stuId
    }
  })
  
  this.getMyCourseDetail()
  },
  // 获取我的课程详情
  async getMyCourseDetail(){
  const {code, data:res} = await MyCourseDetail(this.data.formData)
  if(code === 200) {
    this.setData({
      courseDetail:res,
      courseProportion:(res.usedLessonCount / res.lessonCount)*100+''
    })
  }  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    const title = this.data.title
    const {serialId,stuId} = this.data.formData
   return {
     title:"我的课程",
     path:`/myCourse_package/pages/CourseIntroduction/CourseIntroduction?title=${title}&stuId=${stuId}&serialId=${serialId}`
   }
  }
})